﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Locator.PersonalLocations;
using DataAccess;
using DataAccess.BusinessEntities;
using System.ServiceModel.Activation;
using System.Threading;
using System.Web;
using Locator.GeoLocation.Services;
using Locator.GeoLocation.BusinessEntities;
using DataAccess.Services;

namespace Locator.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PersonalLocationsService : IPersonalLocationsService, IDisposable
    {
        private readonly IPersonalLocationsController personalLocationsController;

        /// <summary>
        /// Initializes a new instance of the PersonalLocationsService class.
        /// </summary>
        public PersonalLocationsService()
            : this(new PersonalLocationsController(new LcboDrinkLocatorContext(), new GeoLocationServiceAgent()))
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the PersonalLocationsService class.
        /// </summary>
        /// <param name="personalLocationsController"></param>
        public PersonalLocationsService(IPersonalLocationsController personalLocationsController)
        {
            this.personalLocationsController = personalLocationsController;
            Thread.CurrentPrincipal = HttpContext.Current.User;
        }

        /// <summary>
        /// Gets the personal locations for the current user.
        /// </summary>
        /// <returns></returns>
        public List<PersonalLocation> GetPersonalLocationsForCurrentUser()
        {
            return this.personalLocationsController.GetPersonalLocationsForCurrentUser();
        }

        /// <summary>
        /// Gets a personal location for the current user.
        /// </summary>
        /// <param name="ID">The ID.</param>
        /// <returns></returns>
        public PersonalLocation GetPersonalLocationForCurrentUser(int ID)
        {
            return this.personalLocationsController.GetPersonalLocationForCurrentUser(ID);
        }

        /// <summary>
        /// Adds a personal location for current user.
        /// </summary>
        /// <param name="description">The description.</param>
        /// <param name="latitude">The latitude.</param>
        /// <param name="longitude">The longitude.</param>
        public void AddPersonalLocationForCurrentUser(string description, double latitude, double longitude)
        {
            this.personalLocationsController.AddPersonalLocationForCurrentUser(description, latitude, longitude);
        }

        /// <summary>
        /// Deletes a personal location for current user.
        /// </summary>
        /// <param name="personalLocationId">The personal location id.</param>
        public void DeletePersonalLocationForCurrentUser(int personalLocationId)
        {
            this.personalLocationsController.DeletePersonalLocationForCurrentUser(personalLocationId);
        }

        public Location SearchByPostalCode(string postalCode, string country)
        {
            return this.personalLocationsController.SearchByPostalCode(postalCode, country);
        }

        public Location SearchByAddress(string streetAddress, string country, string province, string city)
        {
            return this.personalLocationsController.SearchByAddress(streetAddress, country, province, city);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.personalLocationsController != null)
                {
                    this.personalLocationsController.Dispose();
                }
            }
        }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="PersonalLocationsService"/> is reclaimed by garbage collection.
        /// </summary>
        ~PersonalLocationsService()
        {
            this.Dispose(false);
        }
    }
}
